//
//  WmcTagStyleSelectView.swift
//  YPWatermarkCamera
//
//  Created by 赵新 on 2022/3/17.
//

import UIKit

extension WmcTagStyleSelectView {
    class ListModel {
        // Lifecycle

        init(style: WmcBaseTagView.TagStyle,
             image: UIImage,
             isSelect: Bool)
        {
            self.style = style
            self.image = image
            self.isSelect = isSelect
        }

        // Internal

        var style: WmcBaseTagView.TagStyle
        var image: UIImage
        var isSelect: Bool
    }
}

// MARK: - [WmcTagStyleSelectView]

class WmcTagStyleSelectView: UICollectionView {
    // Lifecycle

    convenience init() {
        let frame = CGRect(x: 0, y: 0, width: Wmc_screenWidth, height: 74)
        self.init(frame: frame, collectionViewLayout: Self.layout)
        delegate = self
        dataSource = self
        self.yp.registerCells([
            YPRegister(WmcTagStyleSelectCell.self),
        ])
        backgroundColor = .clear
        self.showsVerticalScrollIndicator = false
        self.showsHorizontalScrollIndicator = false
        
        for (idx, style) in WmcBaseTagView.TagStyle.allCases.enumerated() {
            let model: ListModel = .init(style: style,
                                         image: style.info.perviewImage,
                                         isSelect: idx == 0)
            tagStyleList.append(model)
        }
        
    }

    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        super.init(frame: frame, collectionViewLayout: layout)
    }

    @available(*, unavailable)
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    // Internal

    static var layout: UICollectionViewFlowLayout {
        let layout = UICollectionViewFlowLayout
            .YP.CreateGridLayout(scrollDirection: .horizontal,
                                 minimumLineSpacing: 16,
                                 minimumInteritemSpacing: 0,
                                 sectionInset: Wmc_standardEdge)
        return layout
    }

    var tagStyleList: [ListModel] = []
    /// 样式选择变化
    var styleChangeSelectBlock: YPBlock.OneParam<WmcBaseTagView.TagStyle>?
    
    /// 选中一个样式
    func select(style: WmcBaseTagView.TagStyle) {
        WmcBaseTagView.TagStyle.allCases.enumerated().forEach { (idx, _style) in
            if style == _style {
                select(at: idx)
                return
            }
        }
    }
    
    /// 选中一个
    func select(at index: Int) {
        for (idx, model) in tagStyleList.enumerated() {
            model.isSelect = idx == index
        }
        reloadData()
    }
}

// MARK: - UICollectionViewDelegate

extension WmcTagStyleSelectView: UICollectionViewDelegate {
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        if let model = tagStyleList.yp.object(at: indexPath.row) {
            select(at: indexPath.row)
            YPDevice.PlayShake(.light)
            let style = model.style
            styleChangeSelectBlock?(style)
        }
    }
}

// MARK: - UICollectionViewDelegateFlowLayout

extension WmcTagStyleSelectView: UICollectionViewDelegateFlowLayout {
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        let lineSpacing = Self.layout.minimumLineSpacing * (tagStyleList.count.yp.cgFloat - 1)
        let horizontalEdge = Self.layout.sectionInset.yp.horizontal
        let width = (collectionView.frame.size.width - lineSpacing - horizontalEdge) / tagStyleList.count.yp.cgFloat
        let height = collectionView.frame.size.height - Self.layout.sectionInset.yp.vertical
        return CGSize(width: width + 10, height: height)
    }
}

// MARK: - UICollectionViewDataSource

extension WmcTagStyleSelectView: UICollectionViewDataSource {
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return tagStyleList.count
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell: WmcTagStyleSelectCell = collectionView.yp.dequeueReusableCell(for: indexPath)
        if let model = tagStyleList.yp.object(at: indexPath.row) {
            let image = model.style.info.perviewImage
            cell.imageView.image = image
            Wmc_SpringAnimation() {
                if model.isSelect {
                    cell.borderView.isHidden = false
                    cell.transform = CGAffineTransform.YP.CreateScale(1.2)
                } else {
                    cell.borderView.isHidden = true
                    cell.transform = CGAffineTransform.YP.CreateScale(1)
                }
            }
        }
        return cell
    }
}
